"""
 Copyright 2009 White Waters Software
 White Waters Software Copyright 2007 Jason White

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 CONTACT: whitewaterssoftwareinfo@gmail.com
 
 Medina , Ohio , United States Of America , Solar System , Milky Way Galaxy , The Universe : as I know it
 """
#objects
#classes
import pygame
import random
import sounds
import defs

pygame.init()
index=[]
col=[]
play=0

#player
class player(pygame.sprite.Sprite):
    def __init__(self):
        self.ind=-1 #index on object index needs to be created
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image , self.rect = defs.load_image('player.gif', -1)
        self.x=400
        self.y=580
        self.type="player"
        self.spd=3 #speed
        self.i=0
        self.wait=10
        self.create=120
        self.ii=640
        self.iii=0
        self.j=0
        self.spc=0
        self.time=0
        self.lives=3
        self.score=0
        while self.j < 60:
            ind=sstar()
            index.append(ind)
            ind.ind=ind
            self.j+=1
        self.j=0
            
    def update(self):
        
        self.i+=1
        self.spc+=1
        self.j+=1

        if self.j > 15:
            ind=star()
            index.append(ind)
            ind.ind=ind
            self.j=0
            
        self.xy=pygame.mouse.get_pos()
        if self.xy[0] > 650:
            self.xy=(650,self.xy[1])
            
        if self.xy[0] < 20:
            self.xy=(20,self.xy[1])
            
        self.rect.center = self.xy#(self.x,self.y)
        self.x=pygame.mouse.get_pos()
        
        if self.i > self.wait:
            self.i=self.wait
            
        if self.ii > 0:
            self.ii-=.2
            
        self.iii+=1

        if self.spc==(40*6.5) :
            ind=ship()
            index.append(ind)
            ind.ind=ind

        if self.spc==(40*9) :
            ind=ship()
            index.append(ind)
            ind.ind=ind

        if self.spc==(40*11) :
            ind=ship()
            index.append(ind)
            ind.ind=ind

        if self.iii > self.create+self.ii:
            ind=ship()
            index.append(ind)
            ind.ind=ind
            self.iii=0
            if self.spc > (40*60):
                ind=ship()
                index.append(ind)
                ind.ind=ind
                
            if self.spc  > (40*100):
                ind=ship()
                index.append(ind)
                ind.ind=ind
        self.mouse=pygame.mouse.get_pressed()
        if not self.mouse[0]==0:
            if self.i == self.wait:
                ind=shot((self.xy))
                index.append(ind)
                ind.ind=ind
                self.i=0

        self.ia=-1
        while not self.ia == -2 :
            try:
                if not self.ia == self.id :
                    self.ia+=1
                    if index[self.ia].type=="enemy":
                        if self.rect.colliderect(index[self.ia].rect):
                             self.xy=(0,0)#self.destroy()
            except:
                self.ia=-2
                
    def destroy(self):
        index.remove(self.ind)
        del self
        
#basic ship may fire shots ...
class ship(pygame.sprite.Sprite):
    def __init__(self):
        self.ind=-1 #index on object index needs to be created
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.img=random.choice(["frig1.png","frig2.png","frig3.png","cargo1.png","cargo2.png","cargo3.png","triship1.png","triship2.png","triship3.png"])
        self.image , self.rect = defs.load_image(self.img, -1)
        self.x=random.randint( 20, 650)
        self.y=-24 
        self.spd=2 #speed
        self.i=0
        self.wait=30
        self.lives=3
        self.h=3

        if self.img=="frig1.png":
            self.h=3
            self.spd=random.choice([4,5])
                                                 
        if self.img=="frig2.png":
            self.h=4
            self.spd=random.choice([3,4])
                                                 
        if self.img=="frig3.png":
            self.h=5
            self.spd=random.choice([2,3])
                                                 
        if self.img=="cargo1.png":
            self.h=4
            self.spd=random.choice([4,5])
                                                 
        if self.img=="cargo2.png":
            self.h=5
            self.spd=random.choice([3,4])
                                                 
        if self.img=="cargo3.png":
            self.h=6
            self.spd=random.choice([2,3])
                                                 
        if self.img=="triship1.png":
            self.h=2
            self.spd=random.choice([4,5])
                                                 
        if self.img=="triship2.png":
            self.h=3
            self.spd=random.choice([4])
                                                 
        if self.img=="triship3.png":
            self.h=4
            self.spd=random.choice([3])

        self.spd+=1
                                   
    def update(self):
        self.i+=1
        if self.i > self.wait:
            self.i=self.wait
        if not self.y > 620:
            self.y+=self.spd
        else:
            self.destroy()
            
        self.rect.center = (self.x,self.y)

        if self.h < 1:
            self.destroy()
    def destroy(self):
        if self.h < 1:
            ind=boom((self.x,self.y))
            index.append(ind)
            ind.ind=ind
            sounds.play(sounds.boom)
            play.score+=(250+random.randint( -101, 101))
        index.remove(self.ind)
        del self
        

class shot(pygame.sprite.Sprite):
    
    def __init__(self,xy):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = defs.load_image('shot.png', -1)
        self.xy=xy
        self.own=0
        self.type="enemy"
        self.spd=7
        sounds.play(sounds.shot)

    def update(self):
        self.y=self.xy[1]
        self.x=self.xy[0]
        self.y-=self.spd
        self.xy=(self.x,self.y)
        self.rect.center = (self.xy)

        if self.xy[1] < -3:
            self.destroy()
        i=-1
        while not i == -2 :
            try:
                i+=1
                if self.rect.colliderect(index[i].rect):
                    index[i].h-=1
                    index[i].spd-=.2
                    self.destroy()
            except:
                i=-2
                
    def destroy(self):
        index.remove(self.ind)
        del self

class e_shot(pygame.sprite.Sprite):
    
    def __init__(self,xy):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = defs.load_image('shot.png', -1)
        self.xy=xy
        self.own=0
        self.type="enemy"
        self.spd=7
        sounds.play(sounds.shot)

    def update(self):
        self.y=self.xy[1]
        self.x=self.xy[0]
        self.y+=self.spd
        self.xy=(self.x,self.y)
        self.rect.center = (self.xy)

        if self.xy[1] < -3:
            self.destroy()
        i=-1
        while not i == -2 :
            try:
                i+=1
                if self.rect.colliderect(index[i].rect):
                    if index[i].type=="player" :
                        index[i].lives-=1
                        self.destroy()
            except:
                i=-2
                
    def destroy(self):
        index.remove(self.ind)
        del self

class boom(pygame.sprite.Sprite):
    
    def __init__(self,xy):
        self.ind=None
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = defs.load_image('boom.gif', -1)
        self.xy=xy
        self.rect.center = (self.xy)
        self.i=0
        self.ii=32
        self.type="GFX"
        sounds.play(sounds.shot)

    def update(self):
        self.i+=1
        #self.ii-=2
        #center = self.rect.center
        #self.rect.center = (self.xy)
        #self.image=pygame.transform.scale(self.image, (self.ii, self.ii))
        #self.rect = self.image.get_rect(center=center)
        if self.i > 5:
            self.destroy()

    def destroy(self):
        index.remove(self.ind)
        del self
class star(pygame.sprite.Sprite):
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.img=random.choice(["star.gif","ssstar.gif","sstar.gif"])
        self.image, self.rect = defs.load_image(self.img, -1)
        self.xy=(random.randint(0, 670),-3)
        self.own=0
        self.type="enemy"
        if self.img=="ssstar.gif":
            self.spd=.25
        elif self.img=="sstar.gif":
            self.spd=.5
        else:
            self.spd=1

    def update(self):
        self.y=self.xy[1]
        self.x=self.xy[0]
        self.y+=self.spd
        self.xy=(self.x,self.y)
        self.rect.center = (self.xy)

        if self.xy[1] > 620:
            self.destroy()
                
    def destroy(self):
        index.remove(self.ind)
        del self

class sstar(pygame.sprite.Sprite):
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.img=random.choice(["star.gif","ssstar.gif","sstar.gif"])
        self.image, self.rect = defs.load_image(self.img, -1)
        self.xy=(random.randint(0, 670),random.randint(0, 600))
        self.own=0
        self.type="enemy"
        if self.img=="ssstar.gif":
            self.spd=.25
        elif self.img=="sstar.gif":
            self.spd=.5
        else:
            self.spd=1
            
    def update(self):
        self.y=self.xy[1]
        self.x=self.xy[0]
        self.y+=self.spd
        self.xy=(self.x,self.y)
        self.rect.center = (self.xy)

        if self.xy[1] > 620:
            self.destroy()
                
    def destroy(self):
        index.remove(self.ind)
        del self


